package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
   @Autowired
    private DishMapper dishMapper;




    @Override
    public void saveWithFlavors(DishDto dishDto) {
        //向菜品表添加一条数据 添加之前 ,dishDto是没有主键Id的,保存之后,Id会自动赋值
        this.save(dishDto);
        //向口味表中添加多种数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //Dto给每一个DishFlavor设置dishId
        flavors.forEach(flavor->flavor.setDishId(dishDto.getId()));

        dishFlavorService.saveBatch(flavors);
    }

   //扩展方法,分页查询菜品数据,包扣分类名称
    @Override
    public Page<DishDto> pageDishDto(Integer page, Integer pageSize, String name) {
        //创建分页对象
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        //调用mapper分页查询
        return dishMapper.pageDishDto(pageInfo,name);
    }

    //扩展方法删除菜品
    @Override
    public void deleteByIds(Long[] ids) {
        //根据菜品IDS查询其中是否有菜品起售
        LambdaQueryWrapper<Dish>lqw  = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids).eq(Dish::getStatus,1);
        Integer integer = dishMapper.selectCount(lqw);
        if (integer>0){
            throw new CustomerException("傻逼这个在卖删不了");
        }
        dishFlavorService.lambdaUpdate().in(DishFlavor::getId,ids).remove();
        dishMapper.deleteBatchIds(Arrays.asList(ids));

    }

    //根据id查询菜品详细信息
    @Override
    public DishDto getDishByIdWithFlavor(Long id) {
        DishDto dishDto=dishMapper.getDishByIdWithFlavor(id);
        return dishDto;
    }

    //修改
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //修改dish数据
        this.updateById(dishDto);
        //修改dishFlavor数据  先删除在添加
        dishFlavorService.update()
                        .eq("dish_id",dishDto.getId()).remove();
        List<DishFlavor> flavors = dishDto.getFlavors();
        //Dto给每一个DishFlavor设置dishId
        flavors.forEach(flavor->flavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public List<DishDto> listDishDto(Long categoryId, String name) {
        List<DishDto> dishDtoList=dishMapper.listDishDto(categoryId,name);
        return dishDtoList;
    }

    @Override
    public List<DishDto> getDishSetmealId(Long setmealId) {
        return dishMapper.getDishBySetmealId(setmealId);
    }

}


